###############################################                                     
# Cause of Effect? Turnout in Hispanic Majority-Minority Districts
#  - John A. Henderson, Jasjeet S. Sekhon, and Rocio Titiunik
#  - Forthcoming in Political Analysis
#  - Replication file for <Figure IX>
#  - April 14, 2016
###############################################

############################################################################
#
## GENERATES TABLE IX: Difference Results for Open Seats
#
###########################################################################
        
options(width=150)
rm(list=ls())   

path='/local/'  
source(paste(path,'replicationPA/funs/headers.R',sep=''))
    
set.seed(57459)

# LOAD BASELINE AND MATCHED DATA
            
#~/ethnicity/open_seats/output/RData.WH-data-open_seats
#~/ethnicity/open_seats/output/final_runs/Matched-open_seats-trimmed.csv

#load(paste(path,"RData.WH-data-open_seats",sep='')); WH2data <- data    
load(paste(path,'replicationPA/data/open_data/OpenBaseline-aftermatching.Rdata',sep=''))
WH2data <- WHdata      

Sdata <-  read.csv(file = paste(path,"replicationPA/data/open_data/Matched-open_seats-trimmed.csv",sep=''), header = TRUE, stringsAsFactors = FALSE)
                                                                        
# cheap matching; roughly get at bias   
WH2data=WH2data[order(WH2data$DisTri_1991),] 
       
indtr=matrix(NA,length(which(WH2data$Tr==TRUE)),2) 
indtr[,1]=which(WH2data$Tr==TRUE)
indtr[,2]=WH2data$DisTri_1991[indtr[,1]]    
indtr=indtr[order(indtr[,2]),]
indct=matrix(NA,nrow(indtr),2)                
                       
distrip=WH2data$DisTri_1991     
und=sort(unique(distrip))  
sps=c() 
spa=c()
for(i in 1:length(und)){
	ind=which(distrip==und[i] & WH2data$Tr==FALSE) 
	inda=which(distrip==und[i] & WH2data$Tr==TRUE)
	if(length(ind)>0){ 
		len=length(which(distrip==und[i] & WH2data$Tr==TRUE))
		sps=c(sps,sort(sample(ind,replace=T,size=len))) 
		spa=c(spa,inda)
	}
}   

#indtr[,1]=spa
#indct[,1]=sps
#indct[,2]=distrip[sps]

indt=c(spa,sps)
WH2data=WH2data[indt,]       
 


WH2data$phh_income99_39less  = WH2data$phh_income99_0to19    + WH2data$phh_income99_20to39
WH2data$phh_income99_40to74  = WH2data$phh_income99_40to59   + WH2data$phh_income99_60to74
WH2data$phh_income99_100plus = WH2data$phh_income99_100to199 + WH2data$phh_income99_200
WH2data$ppop_foreign         = WH2data$ppop_foreign_naturcit + WH2data$ppop_foreign_nocit

WH2data$y98_reg_nhisp		 = WH2data$y98_reg_tot - WH2data$y98_reg_hisp 
WH2data$y98_preg_nhisp		 = WH2data$y98_reg_nhisp/WH2data$y98_reg_tot

Sdata$y98_reg_nhisp			 = Sdata$y98_reg_tot - Sdata$y98_reg_hisp
Sdata$y98_preg_nhisp		 = Sdata$y98_reg_nhisp/Sdata$y98_reg_tot
  
   
# WH2data treatment 
Tr <- WH2data$Tr
m1=c()
m1$index.treated=which(Tr==T)
m1$index.control=which(Tr==F)           
print(table(Tr)) 
    
# Sdata treatment 
Tr <- Sdata$Tr
m2=c()
m2$index.treated=which(Tr==T)
m2$index.control=which(Tr==F)           
print(table(Tr))
                          

######################################################
### Results matrix 
######################################################
ACTcolnm <- c("Mean Diff","Diff means p-val")

ACTrownm <- c(
"Hispanic Turnout 2004-2000 Difference WH2",
"Hispanic Registration 2004-2000 Difference WH2",
"NonHispanic Turnout 2004-2000 Difference WH2",
"NonHispanic Registration 2004-2000 Difference WH2",

"Hispanic Turnout 2004-2000 Difference AM",
"Hispanic Registration 2004-2000 Difference AM",
"NonHispanic Turnout 2004-2000 Difference AM",
"NonHispanic Registration 2004-2000 Difference AM"
)

RESACT=list()
RESACT[[1]] <- matrix(data=NA,nrow=length(ACTrownm), ncol=length(ACTcolnm), dimnames = list(ACTrownm,ACTcolnm))

ACTrownm <- c(
"Hispanic Turnout 2002-2000 Difference WH2",
"Hispanic Registration 2002-2000 Difference WH2",
"NonHispanic Turnout 2002-2000 Difference WH2",
"NonHispanic Registration 2002-2000 Difference WH2",


"Hispanic Turnout 2002-2000 Difference AM",
"Hispanic Registration 2002-2000 Difference AM",
"NonHispanic Turnout 2002-2000 Difference AM",
"NonHispanic Registration 2002-2000 Difference AM"
)

#RESACT=list()
RESACT[[2]] <- matrix(data=NA,nrow=length(ACTrownm), ncol=length(ACTcolnm), dimnames = list(ACTrownm,ACTcolnm))
          
ACTrownm <- c(
"Hispanic Turnout 2006-2000 Difference WH2",
"Hispanic Registration 2006-2000 Difference WH2",
"NonHispanic Turnout 2006-2000 Difference WH2",
"NonHispanic Registration 2006-2000 Difference WH2",


"Hispanic Turnout 2006-2000 Difference AM",
"Hispanic Registration 2006-2000 Difference AM",
"NonHispanic Turnout 2006-2000 Difference AM",
"NonHispanic Registration 2006-2000 Difference AM"
)

#RESACT=list()
RESACT[[3]] <- matrix(data=NA,nrow=length(ACTrownm), ncol=length(ACTcolnm), dimnames = list(ACTrownm,ACTcolnm))


######################################################
### Diff-in-Diff Results 
######################################################

######################################################
# A. 2004 - 2000
######################################################

######################################################
### (i) Diff-in-Diff results on Hispanic Registration
######################################################

#BASELINE
                                     
Y1 <- (WH2data$y04_reg_hisp/WH2data$hvap)
Y0 <- (WH2data$y00_reg_hisp/WH2data$hvap)      
          
Y1[which(WH2data$y04_reg_hisp==999999 | WH2data$hvap==0 | WH2data$hvap==999999)]<-NA
Y0[which(WH2data$y00_reg_hisp==999999 | WH2data$hvap==0 | WH2data$hvap==999999)]<-NA
       
print("Percent Overrun")  
print(rbind(c('2004','2000'),round(c(length(which(Y1>1))/length(Y1),length(which(Y0>1))/length(Y0)),digits=4)))
                         
Y1[which(Y1>1)] <- NA
Y0[which(Y0>1)] <- NA

Y1 <- Y1[c(m1$index.treated,m1$index.control)]
Y0 <- Y0[c(m1$index.treated,m1$index.control)]

Tr <- WH2data$Tr == 1
TrWH2<-Tr[c(m1$index.treated,m1$index.control)]


YWH2.1.Tr=Y1[TrWH2]
YWH2.1.Co=Y1[!TrWH2]
YWH2.0.Tr=Y0[TrWH2]
YWH2.0.Co=Y0[!TrWH2]

ddWH2.Tr <- mean(na.rm=T,YWH2.1.Tr) - mean(na.rm=T,YWH2.0.Tr)
ddWH2.Co <- mean(na.rm=T,YWH2.1.Co) - mean(na.rm=T,YWH2.0.Co)
ddWH2 <- ddWH2.Tr - ddWH2.Co 
print(ddWH2)

YY <- c(YWH2.1.Tr, YWH2.1.Co,YWH2.0.Tr, YWH2.0.Co)
time <- c(rep(1,length(c(YWH2.1.Tr, YWH2.1.Co))),rep(0,length(c(YWH2.0.Tr, YWH2.0.Co))))
TT <- c(rep(1,length(YWH2.1.Tr)),rep(0,length(YWH2.1.Co)),rep(1,length(YWH2.0.Tr)),rep(0,length(YWH2.0.Co)))
lmout = summarize.r(lm(YY~time+TT+I(time*TT)))
print(lmout)

DTr = YWH2.1.Tr - YWH2.0.Tr
DCo = YWH2.1.Co - YWH2.0.Co
D1 = YWH2.1.Tr - YWH2.1.Co  
D0 = YWH2.0.Tr - YWH2.0.Co

t = t.test(DTr, DCo)
cat("T-test:\n");  print(t)
ks = ks.boot(DTr, DCo)
cat("KS Boot:\n"); print(ks)

RESACT[[1]]["Hispanic Registration 2004-2000 Difference WH2","Mean Diff"] = lmout$coefficients["I(time * TT)","Estimate"]
RESACT[[1]]["Hispanic Registration 2004-2000 Difference WH2","Diff means p-val"] = lmout$coefficients["I(time * TT)","Pr(>|t|)"]

     

######################################################
### (ii) Diff-in-Diff results on Hispanic Turnout
######################################################

#BASELINE
                                     
Y1 <- (WH2data$y04_turn_hisp/WH2data$y00_reg_hisp)
Y0 <- (WH2data$y00_turn_hisp/WH2data$y00_reg_hisp)

Y1[which(WH2data$y04_turn_hisp==999999 | WH2data$y00_reg_hisp==0 | WH2data$y00_reg_hisp==999999)]<-NA
Y0[which(WH2data$y00_turn_hisp==999999 | WH2data$y00_reg_hisp==0 | WH2data$y00_reg_hisp==999999)]<-NA

Y1 <- Y1[c(m1$index.treated,m1$index.control)]
Y0 <- Y0[c(m1$index.treated,m1$index.control)]

Tr <- WH2data$Tr == 1    
TrWH2<-Tr[c(m1$index.treated,m1$index.control)]


YWH2.1.Tr=Y1[TrWH2]
YWH2.1.Co=Y1[!TrWH2]
YWH2.0.Tr=Y0[TrWH2]
YWH2.0.Co=Y0[!TrWH2]

ddWH2.Tr <- mean(na.rm=T,YWH2.1.Tr) - mean(na.rm=T,YWH2.0.Tr)
ddWH2.Co <- mean(na.rm=T,YWH2.1.Co) - mean(na.rm=T,YWH2.0.Co)
ddWH2 <- ddWH2.Tr - ddWH2.Co 
print(ddWH2)

YY <- c(YWH2.1.Tr, YWH2.1.Co,YWH2.0.Tr, YWH2.0.Co)
time <- c(rep(1,length(c(YWH2.1.Tr, YWH2.1.Co))),rep(0,length(c(YWH2.0.Tr, YWH2.0.Co))))
TT <- c(rep(1,length(YWH2.1.Tr)),rep(0,length(YWH2.1.Co)),rep(1,length(YWH2.0.Tr)),rep(0,length(YWH2.0.Co)))
lmout = summarize.r(lm(YY~time+TT+I(time*TT)))
print(lmout)

DTr = YWH2.1.Tr - YWH2.0.Tr
DCo = YWH2.1.Co - YWH2.0.Co
D1 = YWH2.1.Tr - YWH2.1.Co  
D0 = YWH2.0.Tr - YWH2.0.Co

t = t.test(DTr, DCo)
cat("T-test:\n");  print(t)
ks = ks.boot(DTr, DCo)
cat("KS Boot:\n"); print(ks)

RESACT[[1]]["Hispanic Turnout 2004-2000 Difference WH2","Mean Diff"] = lmout$coefficients["I(time * TT)","Estimate"]
RESACT[[1]]["Hispanic Turnout 2004-2000 Difference WH2","Diff means p-val"] = lmout$coefficients["I(time * TT)","Pr(>|t|)"]


######################################################
### (iii) Diff-in-Diff results on NonHispanic Registration
######################################################

#BASELINE    

Y1 <- (WH2data$y04_reg_nhisp/WH2data$nhvap)
Y0 <- (WH2data$y00_reg_nhisp/WH2data$nhvap)    

Y1[which(WH2data$y04_reg_nhisp==999999 | WH2data$nhvap==0 | WH2data$nhvap==999999)]<-NA
Y0[which(WH2data$y00_reg_nhisp==999999 | WH2data$nhvap==0 | WH2data$nhvap==999999)]<-NA
   
print("Percent Overrun")  
print(rbind(c('2004','2000'),round(c(length(which(Y1>1))/length(Y1),length(which(Y0>1))/length(Y0)),digits=4)))
                         
Y1[which(Y1>1)] <- NA
Y0[which(Y0>1)] <- NA

Y1 <- Y1[c(m1$index.treated,m1$index.control)]
Y0 <- Y0[c(m1$index.treated,m1$index.control)]

Tr <- WH2data$Tr == 1    
TrWH2<-Tr[c(m1$index.treated,m1$index.control)]


YWH2.1.Tr=Y1[TrWH2]
YWH2.1.Co=Y1[!TrWH2]
YWH2.0.Tr=Y0[TrWH2]
YWH2.0.Co=Y0[!TrWH2]


ddWH2.Tr <- mean(na.rm=T,YWH2.1.Tr) - mean(na.rm=T,YWH2.0.Tr)
ddWH2.Co <- mean(na.rm=T,YWH2.1.Co) - mean(na.rm=T,YWH2.0.Co)
ddWH2 <- ddWH2.Tr - ddWH2.Co 
print(ddWH2)

YY <- c(YWH2.1.Tr, YWH2.1.Co,YWH2.0.Tr, YWH2.0.Co)
time <- c(rep(1,length(c(YWH2.1.Tr, YWH2.1.Co))),rep(0,length(c(YWH2.0.Tr, YWH2.0.Co))))
TT <- c(rep(1,length(YWH2.1.Tr)),rep(0,length(YWH2.1.Co)),rep(1,length(YWH2.0.Tr)),rep(0,length(YWH2.0.Co)))
lmout = summarize.r(lm(YY~time+TT+I(time*TT)))
print(lmout)

DTr = YWH2.1.Tr - YWH2.0.Tr
DCo = YWH2.1.Co - YWH2.0.Co
D1 = YWH2.1.Tr - YWH2.1.Co  
D0 = YWH2.0.Tr - YWH2.0.Co

t = t.test(DTr, DCo)
cat("T-test:\n");  print(t)
ks = ks.boot(DTr, DCo)
cat("KS Boot:\n"); print(ks)

RESACT[[1]]["NonHispanic Registration 2004-2000 Difference WH2","Mean Diff"] = lmout$coefficients["I(time * TT)","Estimate"]
RESACT[[1]]["NonHispanic Registration 2004-2000 Difference WH2","Diff means p-val"] = lmout$coefficients["I(time * TT)","Pr(>|t|)"]




######################################################
### (iv) Diff-in-Diff results on NonHispanic Turnout
######################################################

#BASELINE

Y1 <- (WH2data$y04_turn_nhisp/WH2data$y00_reg_nhisp)
Y0 <- (WH2data$y00_turn_nhisp/WH2data$y00_reg_nhisp)

Y1[which(WH2data$y04_turn_nhisp==999999 | WH2data$y00_reg_nhisp==0 | WH2data$y00_reg_nhisp==999999)]<-NA
Y0[which(WH2data$y00_turn_nhisp==999999 | WH2data$y00_reg_nhisp==0 | WH2data$y00_reg_nhisp==999999)]<-NA

Y1 <- Y1[c(m1$index.treated,m1$index.control)]
Y0 <- Y0[c(m1$index.treated,m1$index.control)]

Tr <- WH2data$Tr == 1    
TrWH2<-Tr[c(m1$index.treated,m1$index.control)]


YWH2.1.Tr=Y1[TrWH2]
YWH2.1.Co=Y1[!TrWH2]
YWH2.0.Tr=Y0[TrWH2]
YWH2.0.Co=Y0[!TrWH2]


ddWH2.Tr <- mean(na.rm=T,YWH2.1.Tr) - mean(na.rm=T,YWH2.0.Tr)
ddWH2.Co <- mean(na.rm=T,YWH2.1.Co) - mean(na.rm=T,YWH2.0.Co)
ddWH2 <- ddWH2.Tr - ddWH2.Co 
print(ddWH2)

YY <- c(YWH2.1.Tr, YWH2.1.Co,YWH2.0.Tr, YWH2.0.Co)
time <- c(rep(1,length(c(YWH2.1.Tr, YWH2.1.Co))),rep(0,length(c(YWH2.0.Tr, YWH2.0.Co))))
TT <- c(rep(1,length(YWH2.1.Tr)),rep(0,length(YWH2.1.Co)),rep(1,length(YWH2.0.Tr)),rep(0,length(YWH2.0.Co)))
lmout = summarize.r(lm(YY~time+TT+I(time*TT)))
print(lmout)

DTr = YWH2.1.Tr - YWH2.0.Tr
DCo = YWH2.1.Co - YWH2.0.Co
D1 = YWH2.1.Tr - YWH2.1.Co  
D0 = YWH2.0.Tr - YWH2.0.Co

t = t.test(DTr, DCo)
cat("T-test:\n");  print(t)
ks = ks.boot(DTr, DCo)
cat("KS Boot:\n"); print(ks)

RESACT[[1]]["NonHispanic Turnout 2004-2000 Difference WH2","Mean Diff"] = lmout$coefficients["I(time * TT)","Estimate"]
RESACT[[1]]["NonHispanic Turnout 2004-2000 Difference WH2","Diff means p-val"] = lmout$coefficients["I(time * TT)","Pr(>|t|)"]



######################################################
### (i) Diff-in-Diff results on Hispanic Registration
######################################################

#MATCHED 
    
YSt1 <- (Sdata$y04_reg_hisp/Sdata$hvap)
YSt0 <- (Sdata$y00_reg_hisp/Sdata$hvap)
   
indx <- which(Sdata$hvap==0 | Sdata$hvap==999999 | 
Sdata$y04_reg_hisp==999999 | Sdata$y00_reg_hisp==999999 )
  
YSt1[indx] <- NA
YSt0[indx] <- NA
  
print("Percent Overrun")  
print(rbind(c('2004','2000'),round(c(length(which(YSt1>1))/length(YSt1),length(which(YSt0>1))/length(YSt0)),digits=4)))
                         
YSt1[which(YSt1>1)] <- NA
YSt0[which(YSt0>1)] <- NA

Y1m.Tr<-YSt1[m2$index.treated]
Y1m.Co<-YSt1[m2$index.control]
Y1m  <- c(Y1m.Tr,Y1m.Co)
Tr1m <- c(rep(TRUE,length(Y1m.Tr)),rep(FALSE,length(Y1m.Co)))

Y0m.Tr<-YSt0[m2$index.treated]
Y0m.Co<-YSt0[m2$index.control]
Y0m  <- c(Y0m.Tr,Y0m.Co)
Tr1m <- c(rep(TRUE,length(Y0m.Tr)),rep(FALSE,length(Y0m.Co)))

mddTr <- mean(na.rm=T,Y1m.Tr) - mean(na.rm=T,Y0m.Tr)
mddCo <- mean(na.rm=T,Y1m.Co) - mean(na.rm=T,Y0m.Co)
mdd <- mddTr - mddCo
print(mdd)

YY <- c(Y1m.Tr, Y1m.Co,Y0m.Tr, Y0m.Co)
time <- c(rep(1,length(c(Y1m.Tr, Y1m.Co))),rep(0,length(c(Y0m.Tr, Y0m.Co))))
TT <- c(rep(1,length(Y1m.Tr)),rep(0,length(Y1m.Co)),rep(1,length(Y0m.Tr)),rep(0,length(Y0m.Co)))
lmout = summarize.r(lm(YY~time+TT+I(time*TT)))
print(lmout)

DTr = Y1m.Tr - Y0m.Tr
DCo = Y1m.Co - Y0m.Co
D1 = Y1m.Tr - Y1m.Co
D0 = Y0m.Tr - Y0m.Co

t = t.test(DTr, DCo)
cat("T-test:\n");  print(t)
ks = ks.boot(DTr, DCo)
cat("KS Boot:\n"); print(ks)


RESACT[[1]]["Hispanic Registration 2004-2000 Difference AM","Mean Diff"] = lmout$coefficients["I(time * TT)","Estimate"]
RESACT[[1]]["Hispanic Registration 2004-2000 Difference AM","Diff means p-val"] = lmout$coefficients["I(time * TT)","Pr(>|t|)"]





######################################################
### (ii) Diff-in-Diff results on Hispanic Turnout
######################################################

#MATCHED

YSt1 <- (Sdata$y04_turn_hisp/Sdata$y00_reg_hisp)
YSt0 <- (Sdata$y00_turn_hisp/Sdata$y00_reg_hisp)

indx <- which(Sdata$y00_reg_hisp==0 | Sdata$y00_reg_hisp==999999 | 
Sdata$y04_turn_hisp==999999 | Sdata$y00_turn_hisp==999999 )

YSt1[indx] <- NA
YSt0[indx] <- NA

Y1m.Tr<-YSt1[m2$index.treated]
Y1m.Co<-YSt1[m2$index.control]
Y1m  <- c(Y1m.Tr,Y1m.Co)
Tr1m <- c(rep(TRUE,length(Y1m.Tr)),rep(FALSE,length(Y1m.Co)))

Y0m.Tr<-YSt0[m2$index.treated]
Y0m.Co<-YSt0[m2$index.control]
Y0m  <- c(Y0m.Tr,Y0m.Co)
Tr1m <- c(rep(TRUE,length(Y0m.Tr)),rep(FALSE,length(Y0m.Co)))

mddTr <- mean(na.rm=T,Y1m.Tr) - mean(na.rm=T,Y0m.Tr)
mddCo <- mean(na.rm=T,Y1m.Co) - mean(na.rm=T,Y0m.Co)
mdd <- mddTr - mddCo
print(mdd)

YY <- c(Y1m.Tr, Y1m.Co,Y0m.Tr, Y0m.Co)
time <- c(rep(1,length(c(Y1m.Tr, Y1m.Co))),rep(0,length(c(Y0m.Tr, Y0m.Co))))
TT <- c(rep(1,length(Y1m.Tr)),rep(0,length(Y1m.Co)),rep(1,length(Y0m.Tr)),rep(0,length(Y0m.Co)))
lmout = summarize.r(lm(YY~time+TT+I(time*TT)))
print(lmout)

DTr = Y1m.Tr - Y0m.Tr
DCo = Y1m.Co - Y0m.Co
D1 = Y1m.Tr - Y1m.Co
D0 = Y0m.Tr - Y0m.Co

t = t.test(DTr, DCo)
cat("T-test:\n");  print(t)
ks = ks.boot(DTr, DCo)
cat("KS Boot:\n"); print(ks)


RESACT[[1]]["Hispanic Turnout 2004-2000 Difference AM","Mean Diff"] = lmout$coefficients["I(time * TT)","Estimate"]
RESACT[[1]]["Hispanic Turnout 2004-2000 Difference AM","Diff means p-val"] = lmout$coefficients["I(time * TT)","Pr(>|t|)"]


######################################################
### (iii) Diff-in-Diff results on NonHispanic Registration
######################################################

#MATCHED  
   

YSt1 <- (Sdata$y04_reg_nhisp/Sdata$nhvap)
YSt0 <- (Sdata$y00_reg_nhisp/Sdata$nhvap)
      
indx <- which(Sdata$nhvap==0 | Sdata$nhvap==999999 |
Sdata$y04_reg_nhisp==999999 | Sdata$y00_reg_nhisp==999999 )

YSt1[indx] <- NA
YSt0[indx] <- NA
 
print("Percent Overrun")  
print(rbind(c('2004','2000'),round(c(length(which(YSt1>1))/length(YSt1),length(which(YSt0>1))/length(YSt0)),digits=4)))

YSt1[which(YSt1>1)] <- NA
YSt0[which(YSt0>1)] <- NA

Y1m.Tr<-YSt1[m2$index.treated]
Y1m.Co<-YSt1[m2$index.control]
Y1m  <- c(Y1m.Tr,Y1m.Co)
Tr1m <- c(rep(TRUE,length(Y1m.Tr)),rep(FALSE,length(Y1m.Co)))

Y0m.Tr<-YSt0[m2$index.treated]
Y0m.Co<-YSt0[m2$index.control]
Y0m  <- c(Y0m.Tr,Y0m.Co)
Tr1m <- c(rep(TRUE,length(Y0m.Tr)),rep(FALSE,length(Y0m.Co)))

mddTr <- mean(na.rm=T,Y1m.Tr) - mean(na.rm=T,Y0m.Tr)
mddCo <- mean(na.rm=T,Y1m.Co) - mean(na.rm=T,Y0m.Co)
mdd <- mddTr - mddCo
print(mdd)

YY <- c(Y1m.Tr, Y1m.Co,Y0m.Tr, Y0m.Co)
time <- c(rep(1,length(c(Y1m.Tr, Y1m.Co))),rep(0,length(c(Y0m.Tr, Y0m.Co))))
TT <- c(rep(1,length(Y1m.Tr)),rep(0,length(Y1m.Co)),rep(1,length(Y0m.Tr)),rep(0,length(Y0m.Co)))
lmout = summarize.r(lm(YY~time+TT+I(time*TT)))
print(lmout)

DTr = Y1m.Tr - Y0m.Tr
DCo = Y1m.Co - Y0m.Co
D1 = Y1m.Tr - Y1m.Co
D0 = Y0m.Tr - Y0m.Co

t = t.test(DTr, DCo)
cat("T-test:\n");  print(t)
ks = ks.boot(DTr, DCo)
cat("KS Boot:\n"); print(ks)


RESACT[[1]]["NonHispanic Registration 2004-2000 Difference AM","Mean Diff"] = lmout$coefficients["I(time * TT)","Estimate"]
RESACT[[1]]["NonHispanic Registration 2004-2000 Difference AM","Diff means p-val"] = lmout$coefficients["I(time * TT)","Pr(>|t|)"]


######################################################
### (iv) Diff-in-Diff results on NonHispanic Turnout
######################################################

#MATCHED

YSt1 <- (Sdata$y04_turn_nhisp/Sdata$y00_reg_nhisp)
YSt0 <- (Sdata$y00_turn_nhisp/Sdata$y00_reg_nhisp)

indx <- which(Sdata$y00_reg_nhisp==0 | Sdata$y00_reg_nhisp==999999 |
Sdata$y04_turn_nhisp==999999 | Sdata$y00_turn_nhisp==999999 )

YSt1[indx] <- NA
YSt0[indx] <- NA

Y1m.Tr<-YSt1[m2$index.treated]
Y1m.Co<-YSt1[m2$index.control]
Y1m  <- c(Y1m.Tr,Y1m.Co)
Tr1m <- c(rep(TRUE,length(Y1m.Tr)),rep(FALSE,length(Y1m.Co)))

Y0m.Tr<-YSt0[m2$index.treated]
Y0m.Co<-YSt0[m2$index.control]
Y0m  <- c(Y0m.Tr,Y0m.Co)
Tr1m <- c(rep(TRUE,length(Y0m.Tr)),rep(FALSE,length(Y0m.Co)))

mddTr <- mean(na.rm=T,Y1m.Tr) - mean(na.rm=T,Y0m.Tr)
mddCo <- mean(na.rm=T,Y1m.Co) - mean(na.rm=T,Y0m.Co)
mdd <- mddTr - mddCo
print(mdd)

YY <- c(Y1m.Tr, Y1m.Co,Y0m.Tr, Y0m.Co)
time <- c(rep(1,length(c(Y1m.Tr, Y1m.Co))),rep(0,length(c(Y0m.Tr, Y0m.Co))))
TT <- c(rep(1,length(Y1m.Tr)),rep(0,length(Y1m.Co)),rep(1,length(Y0m.Tr)),rep(0,length(Y0m.Co)))
lmout = summarize.r(lm(YY~time+TT+I(time*TT)))
print(lmout)

DTr = Y1m.Tr - Y0m.Tr
DCo = Y1m.Co - Y0m.Co
D1 = Y1m.Tr - Y1m.Co
D0 = Y0m.Tr - Y0m.Co

t = t.test(DTr, DCo)
cat("T-test:\n");  print(t)
ks = ks.boot(DTr, DCo)
cat("KS Boot:\n"); print(ks)


RESACT[[1]]["NonHispanic Turnout 2004-2000 Difference AM","Mean Diff"] = lmout$coefficients["I(time * TT)","Estimate"]
RESACT[[1]]["NonHispanic Turnout 2004-2000 Difference AM","Diff means p-val"] = lmout$coefficients["I(time * TT)","Pr(>|t|)"]



######################################################
### Diff-in-Diff Results 
######################################################

######################################################
# A. 2002 - 2000
######################################################

######################################################
### (i) Diff-in-Diff results on Hispanic Registration
######################################################


#BASELINE
                                     
Y1 <- (WH2data$y02_reg_hisp/WH2data$hvap)
Y0 <- (WH2data$y00_reg_hisp/WH2data$hvap)
              
Y1[which(WH2data$y02_reg_hisp==999999 | WH2data$hvap==0 | WH2data$hvap==999999)]<-NA
Y0[which(WH2data$y00_reg_hisp==999999 | WH2data$hvap==0 | WH2data$hvap==999999)]<-NA
              
print("Percent Overrun")  
print(rbind(c('2002','2000'),round(c(length(which(Y1>1))/length(Y1),length(which(Y0>1))/length(Y0)),digits=4)))
                         
Y1[which(Y1>1)] <- NA
Y0[which(Y0>1)] <- NA

Y1 <- Y1[c(m1$index.treated,m1$index.control)]
Y0 <- Y0[c(m1$index.treated,m1$index.control)]

Tr <- WH2data$Tr == 1    
TrWH2<-Tr[c(m1$index.treated,m1$index.control)]


YWH2.1.Tr=Y1[TrWH2]
YWH2.1.Co=Y1[!TrWH2]
YWH2.0.Tr=Y0[TrWH2]
YWH2.0.Co=Y0[!TrWH2]

ddWH2.Tr <- mean(na.rm=T,YWH2.1.Tr) - mean(na.rm=T,YWH2.0.Tr)
ddWH2.Co <- mean(na.rm=T,YWH2.1.Co) - mean(na.rm=T,YWH2.0.Co)
ddWH2 <- ddWH2.Tr - ddWH2.Co 
print(ddWH2)

YY <- c(YWH2.1.Tr, YWH2.1.Co,YWH2.0.Tr, YWH2.0.Co)
time <- c(rep(1,length(c(YWH2.1.Tr, YWH2.1.Co))),rep(0,length(c(YWH2.0.Tr, YWH2.0.Co))))
TT <- c(rep(1,length(YWH2.1.Tr)),rep(0,length(YWH2.1.Co)),rep(1,length(YWH2.0.Tr)),rep(0,length(YWH2.0.Co)))
lmout = summarize.r(lm(YY~time+TT+I(time*TT)))
print(lmout)

DTr = YWH2.1.Tr - YWH2.0.Tr
DCo = YWH2.1.Co - YWH2.0.Co
D1 = YWH2.1.Tr - YWH2.1.Co  
D0 = YWH2.0.Tr - YWH2.0.Co

t = t.test(DTr, DCo)
cat("T-test:\n");  print(t)
ks = ks.boot(DTr, DCo)
cat("KS Boot:\n"); print(ks)

RESACT[[2]]["Hispanic Registration 2002-2000 Difference WH2","Mean Diff"] = lmout$coefficients["I(time * TT)","Estimate"]
RESACT[[2]]["Hispanic Registration 2002-2000 Difference WH2","Diff means p-val"] = lmout$coefficients["I(time * TT)","Pr(>|t|)"]

   

######################################################
### (ii) Diff-in-Diff results on Hispanic Turnout
######################################################

#BASELINE
                                     
Y1 <- (WH2data$y02_turn_hisp/WH2data$y00_reg_hisp)
Y0 <- (WH2data$y00_turn_hisp/WH2data$y00_reg_hisp)

Y1[which(WH2data$y02_turn_hisp==999999 | WH2data$y00_reg_hisp==0 | WH2data$y00_reg_hisp==999999)]<-NA
Y0[which(WH2data$y00_turn_hisp==999999 | WH2data$y00_reg_hisp==0 | WH2data$y00_reg_hisp==999999)]<-NA

Y1 <- Y1[c(m1$index.treated,m1$index.control)]
Y0 <- Y0[c(m1$index.treated,m1$index.control)]

Tr <- WH2data$Tr == 1    
TrWH2<-Tr[c(m1$index.treated,m1$index.control)]


YWH2.1.Tr=Y1[TrWH2]
YWH2.1.Co=Y1[!TrWH2]
YWH2.0.Tr=Y0[TrWH2]
YWH2.0.Co=Y0[!TrWH2]

ddWH2.Tr <- mean(na.rm=T,YWH2.1.Tr) - mean(na.rm=T,YWH2.0.Tr)
ddWH2.Co <- mean(na.rm=T,YWH2.1.Co) - mean(na.rm=T,YWH2.0.Co)
ddWH2 <- ddWH2.Tr - ddWH2.Co 
print(ddWH2)

YY <- c(YWH2.1.Tr, YWH2.1.Co,YWH2.0.Tr, YWH2.0.Co)
time <- c(rep(1,length(c(YWH2.1.Tr, YWH2.1.Co))),rep(0,length(c(YWH2.0.Tr, YWH2.0.Co))))
TT <- c(rep(1,length(YWH2.1.Tr)),rep(0,length(YWH2.1.Co)),rep(1,length(YWH2.0.Tr)),rep(0,length(YWH2.0.Co)))
lmout = summarize.r(lm(YY~time+TT+I(time*TT)))
print(lmout)

DTr = YWH2.1.Tr - YWH2.0.Tr
DCo = YWH2.1.Co - YWH2.0.Co
D1 = YWH2.1.Tr - YWH2.1.Co  
D0 = YWH2.0.Tr - YWH2.0.Co

t = t.test(DTr, DCo)
cat("T-test:\n");  print(t)
ks = ks.boot(DTr, DCo)
cat("KS Boot:\n"); print(ks)

RESACT[[2]]["Hispanic Turnout 2002-2000 Difference WH2","Mean Diff"] = lmout$coefficients["I(time * TT)","Estimate"]
RESACT[[2]]["Hispanic Turnout 2002-2000 Difference WH2","Diff means p-val"] = lmout$coefficients["I(time * TT)","Pr(>|t|)"]


######################################################
### (iii) Diff-in-Diff results on NonHispanic Registration
######################################################

#BASELINE    

Y1 <- (WH2data$y02_reg_nhisp/WH2data$nhvap)
Y0 <- (WH2data$y00_reg_nhisp/WH2data$nhvap)

Y1[which(WH2data$y02_reg_nhisp==999999 | WH2data$nhvap==0 | WH2data$nhvap==999999)]<-NA
Y0[which(WH2data$y00_reg_nhisp==999999 | WH2data$nhvap==0 | WH2data$nhvap==999999)]<-NA

print("Percent Overrun")  
print(rbind(c('2002','2000'),round(c(length(which(Y1>1))/length(Y1),length(which(Y0>1))/length(Y0)),digits=4)))

Y1[which(Y1>1)] <- NA
Y0[which(Y0>1)] <- NA
		
Y1 <- Y1[c(m1$index.treated,m1$index.control)]
Y0 <- Y0[c(m1$index.treated,m1$index.control)]

Tr <- WH2data$Tr == 1    
TrWH2<-Tr[c(m1$index.treated,m1$index.control)]


YWH2.1.Tr=Y1[TrWH2]
YWH2.1.Co=Y1[!TrWH2]
YWH2.0.Tr=Y0[TrWH2]
YWH2.0.Co=Y0[!TrWH2]


ddWH2.Tr <- mean(na.rm=T,YWH2.1.Tr) - mean(na.rm=T,YWH2.0.Tr)
ddWH2.Co <- mean(na.rm=T,YWH2.1.Co) - mean(na.rm=T,YWH2.0.Co)
ddWH2 <- ddWH2.Tr - ddWH2.Co 
print(ddWH2)

YY <- c(YWH2.1.Tr, YWH2.1.Co,YWH2.0.Tr, YWH2.0.Co)
time <- c(rep(1,length(c(YWH2.1.Tr, YWH2.1.Co))),rep(0,length(c(YWH2.0.Tr, YWH2.0.Co))))
TT <- c(rep(1,length(YWH2.1.Tr)),rep(0,length(YWH2.1.Co)),rep(1,length(YWH2.0.Tr)),rep(0,length(YWH2.0.Co)))
lmout = summarize.r(lm(YY~time+TT+I(time*TT)))
print(lmout)

DTr = YWH2.1.Tr - YWH2.0.Tr
DCo = YWH2.1.Co - YWH2.0.Co
D1 = YWH2.1.Tr - YWH2.1.Co  
D0 = YWH2.0.Tr - YWH2.0.Co

t = t.test(DTr, DCo)
cat("T-test:\n");  print(t)
ks = ks.boot(DTr, DCo)
cat("KS Boot:\n"); print(ks)

RESACT[[2]]["NonHispanic Registration 2002-2000 Difference WH2","Mean Diff"] = lmout$coefficients["I(time * TT)","Estimate"]
RESACT[[2]]["NonHispanic Registration 2002-2000 Difference WH2","Diff means p-val"] = lmout$coefficients["I(time * TT)","Pr(>|t|)"]



######################################################
### (iv) Diff-in-Diff results on NonHispanic Turnout
######################################################

#BASELINE

Y1 <- (WH2data$y02_turn_nhisp/WH2data$y00_reg_nhisp)
Y0 <- (WH2data$y00_turn_nhisp/WH2data$y00_reg_nhisp)

Y1[which(WH2data$y02_turn_nhisp==999999 | WH2data$y00_reg_nhisp==0 | WH2data$y00_reg_nhisp==999999)]<-NA
Y0[which(WH2data$y00_turn_nhisp==999999 | WH2data$y00_reg_nhisp==0 | WH2data$y00_reg_nhisp==999999)]<-NA

Y1 <- Y1[c(m1$index.treated,m1$index.control)]
Y0 <- Y0[c(m1$index.treated,m1$index.control)]

Tr <- WH2data$Tr == 1    
TrWH2<-Tr[c(m1$index.treated,m1$index.control)]


YWH2.1.Tr=Y1[TrWH2]
YWH2.1.Co=Y1[!TrWH2]
YWH2.0.Tr=Y0[TrWH2]
YWH2.0.Co=Y0[!TrWH2]


ddWH2.Tr <- mean(na.rm=T,YWH2.1.Tr) - mean(na.rm=T,YWH2.0.Tr)
ddWH2.Co <- mean(na.rm=T,YWH2.1.Co) - mean(na.rm=T,YWH2.0.Co)
ddWH2 <- ddWH2.Tr - ddWH2.Co 
print(ddWH2)

YY <- c(YWH2.1.Tr, YWH2.1.Co,YWH2.0.Tr, YWH2.0.Co)
time <- c(rep(1,length(c(YWH2.1.Tr, YWH2.1.Co))),rep(0,length(c(YWH2.0.Tr, YWH2.0.Co))))
TT <- c(rep(1,length(YWH2.1.Tr)),rep(0,length(YWH2.1.Co)),rep(1,length(YWH2.0.Tr)),rep(0,length(YWH2.0.Co)))
lmout = summarize.r(lm(YY~time+TT+I(time*TT)))
print(lmout)

DTr = YWH2.1.Tr - YWH2.0.Tr
DCo = YWH2.1.Co - YWH2.0.Co
D1 = YWH2.1.Tr - YWH2.1.Co  
D0 = YWH2.0.Tr - YWH2.0.Co

t = t.test(DTr, DCo)
cat("T-test:\n");  print(t)
ks = ks.boot(DTr, DCo)
cat("KS Boot:\n"); print(ks)

RESACT[[2]]["NonHispanic Turnout 2002-2000 Difference WH2","Mean Diff"] = lmout$coefficients["I(time * TT)","Estimate"]
RESACT[[2]]["NonHispanic Turnout 2002-2000 Difference WH2","Diff means p-val"] = lmout$coefficients["I(time * TT)","Pr(>|t|)"]



######################################################
### (i) Diff-in-Diff results on Hispanic Registration
######################################################

#MATCHED 
    
YSt1 <- (Sdata$y02_reg_hisp/Sdata$hvap)
YSt0 <- (Sdata$y00_reg_hisp/Sdata$hvap)

indx <- which(Sdata$hvap==0 | Sdata$hvap==999999 | 
Sdata$y02_reg_hisp==999999 | Sdata$y00_reg_hisp==999999 )

YSt1[indx] <- NA
YSt0[indx] <- NA

print("Percent Overrun")  
print(rbind(c('2002','2000'),round(c(length(which(YSt1>1))/length(YSt1),length(which(YSt0>1))/length(YSt0)),digits=4)))

YSt1[which(YSt1>1)] <- NA
YSt0[which(YSt0>1)] <- NA          

Y1m.Tr<-YSt1[m2$index.treated]
Y1m.Co<-YSt1[m2$index.control]
Y1m  <- c(Y1m.Tr,Y1m.Co)
Tr1m <- c(rep(TRUE,length(Y1m.Tr)),rep(FALSE,length(Y1m.Co)))

Y0m.Tr<-YSt0[m2$index.treated]
Y0m.Co<-YSt0[m2$index.control]
Y0m  <- c(Y0m.Tr,Y0m.Co)
Tr1m <- c(rep(TRUE,length(Y0m.Tr)),rep(FALSE,length(Y0m.Co)))

mddTr <- mean(na.rm=T,Y1m.Tr) - mean(na.rm=T,Y0m.Tr)
mddCo <- mean(na.rm=T,Y1m.Co) - mean(na.rm=T,Y0m.Co)
mdd <- mddTr - mddCo
print(mdd)

YY <- c(Y1m.Tr, Y1m.Co,Y0m.Tr, Y0m.Co)
time <- c(rep(1,length(c(Y1m.Tr, Y1m.Co))),rep(0,length(c(Y0m.Tr, Y0m.Co))))
TT <- c(rep(1,length(Y1m.Tr)),rep(0,length(Y1m.Co)),rep(1,length(Y0m.Tr)),rep(0,length(Y0m.Co)))
lmout = summarize.r(lm(YY~time+TT+I(time*TT)))
print(lmout)

DTr = Y1m.Tr - Y0m.Tr
DCo = Y1m.Co - Y0m.Co
D1 = Y1m.Tr - Y1m.Co
D0 = Y0m.Tr - Y0m.Co

t = t.test(DTr, DCo)
cat("T-test:\n");  print(t)
ks = ks.boot(DTr, DCo)
cat("KS Boot:\n"); print(ks)


RESACT[[2]]["Hispanic Registration 2002-2000 Difference AM","Mean Diff"] = lmout$coefficients["I(time * TT)","Estimate"]
RESACT[[2]]["Hispanic Registration 2002-2000 Difference AM","Diff means p-val"] = lmout$coefficients["I(time * TT)","Pr(>|t|)"]




######################################################
### (ii) Diff-in-Diff results on Hispanic Turnout
######################################################

#MATCHED

YSt1 <- (Sdata$y02_turn_hisp/Sdata$y00_reg_hisp)
YSt0 <- (Sdata$y00_turn_hisp/Sdata$y00_reg_hisp)

indx <- which(Sdata$y00_reg_hisp==0 | Sdata$y00_reg_hisp==999999 | 
Sdata$y02_turn_hisp==999999 | Sdata$y00_turn_hisp==999999 )

YSt1[indx] <- NA
YSt0[indx] <- NA

Y1m.Tr<-YSt1[m2$index.treated]
Y1m.Co<-YSt1[m2$index.control]
Y1m  <- c(Y1m.Tr,Y1m.Co)
Tr1m <- c(rep(TRUE,length(Y1m.Tr)),rep(FALSE,length(Y1m.Co)))

Y0m.Tr<-YSt0[m2$index.treated]
Y0m.Co<-YSt0[m2$index.control]
Y0m  <- c(Y0m.Tr,Y0m.Co)
Tr1m <- c(rep(TRUE,length(Y0m.Tr)),rep(FALSE,length(Y0m.Co)))

mddTr <- mean(na.rm=T,Y1m.Tr) - mean(na.rm=T,Y0m.Tr)
mddCo <- mean(na.rm=T,Y1m.Co) - mean(na.rm=T,Y0m.Co)
mdd <- mddTr - mddCo
print(mdd)

YY <- c(Y1m.Tr, Y1m.Co,Y0m.Tr, Y0m.Co)
time <- c(rep(1,length(c(Y1m.Tr, Y1m.Co))),rep(0,length(c(Y0m.Tr, Y0m.Co))))
TT <- c(rep(1,length(Y1m.Tr)),rep(0,length(Y1m.Co)),rep(1,length(Y0m.Tr)),rep(0,length(Y0m.Co)))
lmout = summarize.r(lm(YY~time+TT+I(time*TT)))
print(lmout)

DTr = Y1m.Tr - Y0m.Tr
DCo = Y1m.Co - Y0m.Co
D1 = Y1m.Tr - Y1m.Co
D0 = Y0m.Tr - Y0m.Co

t = t.test(DTr, DCo)
cat("T-test:\n");  print(t)
ks = ks.boot(DTr, DCo)
cat("KS Boot:\n"); print(ks)


RESACT[[2]]["Hispanic Turnout 2002-2000 Difference AM","Mean Diff"] = lmout$coefficients["I(time * TT)","Estimate"]
RESACT[[2]]["Hispanic Turnout 2002-2000 Difference AM","Diff means p-val"] = lmout$coefficients["I(time * TT)","Pr(>|t|)"]


######################################################
### (iii) Diff-in-Diff results on NonHispanic Registration
######################################################

#MATCHED  
   

YSt1 <- (Sdata$y02_reg_nhisp/Sdata$nhvap)
YSt0 <- (Sdata$y00_reg_nhisp/Sdata$nhvap)

indx <- which(Sdata$nhvap==0 | Sdata$nhvap==999999 |
Sdata$y02_reg_nhisp==999999 | Sdata$y00_reg_nhisp==999999 )

YSt1[indx] <- NA
YSt0[indx] <- NA
   
print("Percent Overrun")  
print(rbind(c('2002','2000'),round(c(length(which(YSt1>1))/length(YSt1),length(which(YSt0>1))/length(YSt0)),digits=4)))

YSt1[which(YSt1>1)] <- NA
YSt0[which(YSt0>1)] <- NA

Y1m.Tr<-YSt1[m2$index.treated]
Y1m.Co<-YSt1[m2$index.control]
Y1m  <- c(Y1m.Tr,Y1m.Co)
Tr1m <- c(rep(TRUE,length(Y1m.Tr)),rep(FALSE,length(Y1m.Co)))

Y0m.Tr<-YSt0[m2$index.treated]
Y0m.Co<-YSt0[m2$index.control]
Y0m  <- c(Y0m.Tr,Y0m.Co)
Tr1m <- c(rep(TRUE,length(Y0m.Tr)),rep(FALSE,length(Y0m.Co)))

mddTr <- mean(na.rm=T,Y1m.Tr) - mean(na.rm=T,Y0m.Tr)
mddCo <- mean(na.rm=T,Y1m.Co) - mean(na.rm=T,Y0m.Co)
mdd <- mddTr - mddCo
print(mdd)

YY <- c(Y1m.Tr, Y1m.Co,Y0m.Tr, Y0m.Co)
time <- c(rep(1,length(c(Y1m.Tr, Y1m.Co))),rep(0,length(c(Y0m.Tr, Y0m.Co))))
TT <- c(rep(1,length(Y1m.Tr)),rep(0,length(Y1m.Co)),rep(1,length(Y0m.Tr)),rep(0,length(Y0m.Co)))
lmout = summarize.r(lm(YY~time+TT+I(time*TT)))
print(lmout)

DTr = Y1m.Tr - Y0m.Tr
DCo = Y1m.Co - Y0m.Co
D1 = Y1m.Tr - Y1m.Co
D0 = Y0m.Tr - Y0m.Co

t = t.test(DTr, DCo)
cat("T-test:\n");  print(t)
ks = ks.boot(DTr, DCo)
cat("KS Boot:\n"); print(ks)


RESACT[[2]]["NonHispanic Registration 2002-2000 Difference AM","Mean Diff"] = lmout$coefficients["I(time * TT)","Estimate"]
RESACT[[2]]["NonHispanic Registration 2002-2000 Difference AM","Diff means p-val"] = lmout$coefficients["I(time * TT)","Pr(>|t|)"]


######################################################
### (iv) Diff-in-Diff results on NonHispanic Turnout
######################################################

#MATCHED

YSt1 <- (Sdata$y02_turn_nhisp/Sdata$y00_reg_nhisp)
YSt0 <- (Sdata$y00_turn_nhisp/Sdata$y00_reg_nhisp)

indx <- which(Sdata$y00_reg_nhisp==0 | Sdata$y00_reg_nhisp==999999 |
Sdata$y02_turn_nhisp==999999 | Sdata$y00_turn_nhisp==999999 )

YSt1[indx] <- NA
YSt0[indx] <- NA

Y1m.Tr<-YSt1[m2$index.treated]
Y1m.Co<-YSt1[m2$index.control]
Y1m  <- c(Y1m.Tr,Y1m.Co)
Tr1m <- c(rep(TRUE,length(Y1m.Tr)),rep(FALSE,length(Y1m.Co)))

Y0m.Tr<-YSt0[m2$index.treated]
Y0m.Co<-YSt0[m2$index.control]
Y0m  <- c(Y0m.Tr,Y0m.Co)
Tr1m <- c(rep(TRUE,length(Y0m.Tr)),rep(FALSE,length(Y0m.Co)))

mddTr <- mean(na.rm=T,Y1m.Tr) - mean(na.rm=T,Y0m.Tr)
mddCo <- mean(na.rm=T,Y1m.Co) - mean(na.rm=T,Y0m.Co)
mdd <- mddTr - mddCo
print(mdd)

YY <- c(Y1m.Tr, Y1m.Co,Y0m.Tr, Y0m.Co)
time <- c(rep(1,length(c(Y1m.Tr, Y1m.Co))),rep(0,length(c(Y0m.Tr, Y0m.Co))))
TT <- c(rep(1,length(Y1m.Tr)),rep(0,length(Y1m.Co)),rep(1,length(Y0m.Tr)),rep(0,length(Y0m.Co)))
lmout = summarize.r(lm(YY~time+TT+I(time*TT)))
print(lmout)

DTr = Y1m.Tr - Y0m.Tr
DCo = Y1m.Co - Y0m.Co
D1 = Y1m.Tr - Y1m.Co
D0 = Y0m.Tr - Y0m.Co

t = t.test(DTr, DCo)
cat("T-test:\n");  print(t)
ks = ks.boot(DTr, DCo)
cat("KS Boot:\n"); print(ks)


RESACT[[2]]["NonHispanic Turnout 2002-2000 Difference AM","Mean Diff"] = lmout$coefficients["I(time * TT)","Estimate"]
RESACT[[2]]["NonHispanic Turnout 2002-2000 Difference AM","Diff means p-val"] = lmout$coefficients["I(time * TT)","Pr(>|t|)"]



######################################################
### Diff-in-Diff Results 
######################################################

######################################################
# A. 2006 - 2000
######################################################

######################################################
### (i) Diff-in-Diff results on Hispanic Registration
######################################################


#BASELINE
                                     
Y1 <- (WH2data$y06_reg_hisp/WH2data$hvap)
Y0 <- (WH2data$y00_reg_hisp/WH2data$hvap)

Y1[which(WH2data$y06_reg_hisp==999999 | WH2data$hvap==0 | WH2data$hvap==999999)]<-NA
Y0[which(WH2data$y00_reg_hisp==999999 | WH2data$hvap==0 | WH2data$hvap==999999)]<-NA
        
print("Percent Overrun")  
print(rbind(c('2006','2000'),round(c(length(which(Y1>1))/length(Y1),length(which(Y0>1))/length(Y0)),digits=4)))
                         
Y1[which(Y1>1)] <- NA
Y0[which(Y0>1)] <- NA

Y1 <- Y1[c(m1$index.treated,m1$index.control)]
Y0 <- Y0[c(m1$index.treated,m1$index.control)]

Tr <- WH2data$Tr == 1    
TrWH2<-Tr[c(m1$index.treated,m1$index.control)]


YWH2.1.Tr=Y1[TrWH2]
YWH2.1.Co=Y1[!TrWH2]
YWH2.0.Tr=Y0[TrWH2]
YWH2.0.Co=Y0[!TrWH2]

ddWH2.Tr <- mean(na.rm=T,YWH2.1.Tr) - mean(na.rm=T,YWH2.0.Tr)
ddWH2.Co <- mean(na.rm=T,YWH2.1.Co) - mean(na.rm=T,YWH2.0.Co)
ddWH2 <- ddWH2.Tr - ddWH2.Co 
print(ddWH2)

YY <- c(YWH2.1.Tr, YWH2.1.Co,YWH2.0.Tr, YWH2.0.Co)
time <- c(rep(1,length(c(YWH2.1.Tr, YWH2.1.Co))),rep(0,length(c(YWH2.0.Tr, YWH2.0.Co))))
TT <- c(rep(1,length(YWH2.1.Tr)),rep(0,length(YWH2.1.Co)),rep(1,length(YWH2.0.Tr)),rep(0,length(YWH2.0.Co)))
lmout = summarize.r(lm(YY~time+TT+I(time*TT)))
print(lmout)

DTr = YWH2.1.Tr - YWH2.0.Tr
DCo = YWH2.1.Co - YWH2.0.Co
D1 = YWH2.1.Tr - YWH2.1.Co  
D0 = YWH2.0.Tr - YWH2.0.Co

t = t.test(DTr, DCo)
cat("T-test:\n");  print(t)
ks = ks.boot(DTr, DCo)
cat("KS Boot:\n"); print(ks)

RESACT[[3]]["Hispanic Registration 2006-2000 Difference WH2","Mean Diff"] = lmout$coefficients["I(time * TT)","Estimate"]
RESACT[[3]]["Hispanic Registration 2006-2000 Difference WH2","Diff means p-val"] = lmout$coefficients["I(time * TT)","Pr(>|t|)"]


######################################################
### (ii) Diff-in-Diff results on Hispanic Turnout
######################################################

#BASELINE
                                     
Y1 <- (WH2data$y06_turn_hisp/WH2data$y00_reg_hisp)
Y0 <- (WH2data$y00_turn_hisp/WH2data$y00_reg_hisp)

Y1[which(WH2data$y06_turn_hisp==999999 | WH2data$y00_reg_hisp==0 | WH2data$y00_reg_hisp==999999)]<-NA
Y0[which(WH2data$y00_turn_hisp==999999 | WH2data$y00_reg_hisp==0 | WH2data$y00_reg_hisp==999999)]<-NA

Y1 <- Y1[c(m1$index.treated,m1$index.control)]
Y0 <- Y0[c(m1$index.treated,m1$index.control)]

Tr <- WH2data$Tr == 1    
TrWH2<-Tr[c(m1$index.treated,m1$index.control)]


YWH2.1.Tr=Y1[TrWH2]
YWH2.1.Co=Y1[!TrWH2]
YWH2.0.Tr=Y0[TrWH2]
YWH2.0.Co=Y0[!TrWH2]

ddWH2.Tr <- mean(na.rm=T,YWH2.1.Tr) - mean(na.rm=T,YWH2.0.Tr)
ddWH2.Co <- mean(na.rm=T,YWH2.1.Co) - mean(na.rm=T,YWH2.0.Co)
ddWH2 <- ddWH2.Tr - ddWH2.Co 
print(ddWH2)

YY <- c(YWH2.1.Tr, YWH2.1.Co,YWH2.0.Tr, YWH2.0.Co)
time <- c(rep(1,length(c(YWH2.1.Tr, YWH2.1.Co))),rep(0,length(c(YWH2.0.Tr, YWH2.0.Co))))
TT <- c(rep(1,length(YWH2.1.Tr)),rep(0,length(YWH2.1.Co)),rep(1,length(YWH2.0.Tr)),rep(0,length(YWH2.0.Co)))
lmout = summarize.r(lm(YY~time+TT+I(time*TT)))
print(lmout)

DTr = YWH2.1.Tr - YWH2.0.Tr
DCo = YWH2.1.Co - YWH2.0.Co
D1 = YWH2.1.Tr - YWH2.1.Co  
D0 = YWH2.0.Tr - YWH2.0.Co

t = t.test(DTr, DCo)
cat("T-test:\n");  print(t)
ks = ks.boot(DTr, DCo)
cat("KS Boot:\n"); print(ks)

RESACT[[3]]["Hispanic Turnout 2006-2000 Difference WH2","Mean Diff"] = lmout$coefficients["I(time * TT)","Estimate"]
RESACT[[3]]["Hispanic Turnout 2006-2000 Difference WH2","Diff means p-val"] = lmout$coefficients["I(time * TT)","Pr(>|t|)"]


######################################################
### (iii) Diff-in-Diff results on NonHispanic Registration
######################################################


#BASELINE    

Y1 <- (WH2data$y06_reg_nhisp/WH2data$nhvap)
Y0 <- (WH2data$y00_reg_nhisp/WH2data$nhvap)

Y1[which(WH2data$y06_reg_nhisp==999999 | WH2data$nhvap==0 | WH2data$nhvap==999999)]<-NA
Y0[which(WH2data$y00_reg_nhisp==999999 | WH2data$nhvap==0 | WH2data$nhvap==999999)]<-NA
  
print("Percent Overrun")  
print(rbind(c('2006','2000'),round(c(length(which(Y1>1))/length(Y1),length(which(Y0>1))/length(Y0)),digits=4)))
                         
Y1[which(Y1>1)] <- NA
Y0[which(Y0>1)] <- NA

Y1 <- Y1[c(m1$index.treated,m1$index.control)]
Y0 <- Y0[c(m1$index.treated,m1$index.control)]

Tr <- WH2data$Tr == 1    
TrWH2<-Tr[c(m1$index.treated,m1$index.control)]


YWH2.1.Tr=Y1[TrWH2]
YWH2.1.Co=Y1[!TrWH2]
YWH2.0.Tr=Y0[TrWH2]
YWH2.0.Co=Y0[!TrWH2]


ddWH2.Tr <- mean(na.rm=T,YWH2.1.Tr) - mean(na.rm=T,YWH2.0.Tr)
ddWH2.Co <- mean(na.rm=T,YWH2.1.Co) - mean(na.rm=T,YWH2.0.Co)
ddWH2 <- ddWH2.Tr - ddWH2.Co 
print(ddWH2)

YY <- c(YWH2.1.Tr, YWH2.1.Co,YWH2.0.Tr, YWH2.0.Co)
time <- c(rep(1,length(c(YWH2.1.Tr, YWH2.1.Co))),rep(0,length(c(YWH2.0.Tr, YWH2.0.Co))))
TT <- c(rep(1,length(YWH2.1.Tr)),rep(0,length(YWH2.1.Co)),rep(1,length(YWH2.0.Tr)),rep(0,length(YWH2.0.Co)))
lmout = summarize.r(lm(YY~time+TT+I(time*TT)))
print(lmout)

DTr = YWH2.1.Tr - YWH2.0.Tr
DCo = YWH2.1.Co - YWH2.0.Co
D1 = YWH2.1.Tr - YWH2.1.Co  
D0 = YWH2.0.Tr - YWH2.0.Co

t = t.test(DTr, DCo)
cat("T-test:\n");  print(t)
ks = ks.boot(DTr, DCo)
cat("KS Boot:\n"); print(ks)

RESACT[[3]]["NonHispanic Registration 2006-2000 Difference WH2","Mean Diff"] = lmout$coefficients["I(time * TT)","Estimate"]
RESACT[[3]]["NonHispanic Registration 2006-2000 Difference WH2","Diff means p-val"] = lmout$coefficients["I(time * TT)","Pr(>|t|)"]




######################################################
### (iv) Diff-in-Diff results on NonHispanic Turnout
######################################################

#BASELINE

Y1 <- (WH2data$y06_turn_nhisp/WH2data$y00_reg_nhisp)
Y0 <- (WH2data$y00_turn_nhisp/WH2data$y00_reg_nhisp)

Y1[which(WH2data$y06_turn_nhisp==999999 | WH2data$y00_reg_nhisp==0 | WH2data$y00_reg_nhisp==999999)]<-NA
Y0[which(WH2data$y00_turn_nhisp==999999 | WH2data$y00_reg_nhisp==0 | WH2data$y00_reg_nhisp==999999)]<-NA

Y1 <- Y1[c(m1$index.treated,m1$index.control)]
Y0 <- Y0[c(m1$index.treated,m1$index.control)]

Tr <- WH2data$Tr == 1    
TrWH2<-Tr[c(m1$index.treated,m1$index.control)]


YWH2.1.Tr=Y1[TrWH2]
YWH2.1.Co=Y1[!TrWH2]
YWH2.0.Tr=Y0[TrWH2]
YWH2.0.Co=Y0[!TrWH2]


ddWH2.Tr <- mean(na.rm=T,YWH2.1.Tr) - mean(na.rm=T,YWH2.0.Tr)
ddWH2.Co <- mean(na.rm=T,YWH2.1.Co) - mean(na.rm=T,YWH2.0.Co)
ddWH2 <- ddWH2.Tr - ddWH2.Co 
print(ddWH2)

YY <- c(YWH2.1.Tr, YWH2.1.Co,YWH2.0.Tr, YWH2.0.Co)
time <- c(rep(1,length(c(YWH2.1.Tr, YWH2.1.Co))),rep(0,length(c(YWH2.0.Tr, YWH2.0.Co))))
TT <- c(rep(1,length(YWH2.1.Tr)),rep(0,length(YWH2.1.Co)),rep(1,length(YWH2.0.Tr)),rep(0,length(YWH2.0.Co)))
lmout = summarize.r(lm(YY~time+TT+I(time*TT)))
print(lmout)

DTr = YWH2.1.Tr - YWH2.0.Tr
DCo = YWH2.1.Co - YWH2.0.Co
D1 = YWH2.1.Tr - YWH2.1.Co  
D0 = YWH2.0.Tr - YWH2.0.Co

t = t.test(DTr, DCo)
cat("T-test:\n");  print(t)
ks = ks.boot(DTr, DCo)
cat("KS Boot:\n"); print(ks)

RESACT[[3]]["NonHispanic Turnout 2006-2000 Difference WH2","Mean Diff"] = lmout$coefficients["I(time * TT)","Estimate"]
RESACT[[3]]["NonHispanic Turnout 2006-2000 Difference WH2","Diff means p-val"] = lmout$coefficients["I(time * TT)","Pr(>|t|)"]



######################################################
### (i) Diff-in-Diff results on Hispanic Registration
######################################################

#MATCHED 
    
YSt1 <- (Sdata$y06_reg_hisp/Sdata$hvap)
YSt0 <- (Sdata$y00_reg_hisp/Sdata$hvap)

indx <- which(Sdata$hvap==0 | Sdata$hvap==999999 | 
Sdata$y06_reg_hisp==999999 | Sdata$y00_reg_hisp==999999 )

YSt1[indx] <- NA
YSt0[indx] <- NA
  
print("Percent Overrun")  
print(rbind(c('2006','2000'),round(c(length(which(YSt1>1))/length(YSt1),length(which(YSt0>1))/length(YSt0)),digits=4)))

YSt1[which(YSt1>1)] <- NA
YSt0[which(YSt0>1)] <- NA

Y1m.Tr<-YSt1[m2$index.treated]
Y1m.Co<-YSt1[m2$index.control]
Y1m  <- c(Y1m.Tr,Y1m.Co)
Tr1m <- c(rep(TRUE,length(Y1m.Tr)),rep(FALSE,length(Y1m.Co)))

Y0m.Tr<-YSt0[m2$index.treated]
Y0m.Co<-YSt0[m2$index.control]
Y0m  <- c(Y0m.Tr,Y0m.Co)
Tr1m <- c(rep(TRUE,length(Y0m.Tr)),rep(FALSE,length(Y0m.Co)))

mddTr <- mean(na.rm=T,Y1m.Tr) - mean(na.rm=T,Y0m.Tr)
mddCo <- mean(na.rm=T,Y1m.Co) - mean(na.rm=T,Y0m.Co)
mdd <- mddTr - mddCo
print(mdd)

YY <- c(Y1m.Tr, Y1m.Co,Y0m.Tr, Y0m.Co)
time <- c(rep(1,length(c(Y1m.Tr, Y1m.Co))),rep(0,length(c(Y0m.Tr, Y0m.Co))))
TT <- c(rep(1,length(Y1m.Tr)),rep(0,length(Y1m.Co)),rep(1,length(Y0m.Tr)),rep(0,length(Y0m.Co)))
lmout = summarize.r(lm(YY~time+TT+I(time*TT)))
print(lmout)

DTr = Y1m.Tr - Y0m.Tr
DCo = Y1m.Co - Y0m.Co
D1 = Y1m.Tr - Y1m.Co
D0 = Y0m.Tr - Y0m.Co

t = t.test(DTr, DCo)
cat("T-test:\n");  print(t)
ks = ks.boot(DTr, DCo)
cat("KS Boot:\n"); print(ks)


RESACT[[3]]["Hispanic Registration 2006-2000 Difference AM","Mean Diff"] = lmout$coefficients["I(time * TT)","Estimate"]
RESACT[[3]]["Hispanic Registration 2006-2000 Difference AM","Diff means p-val"] = lmout$coefficients["I(time * TT)","Pr(>|t|)"]





######################################################
### (ii) Diff-in-Diff results on Hispanic Turnout
######################################################

#MATCHED

YSt1 <- (Sdata$y06_turn_hisp/Sdata$y00_reg_hisp)
YSt0 <- (Sdata$y00_turn_hisp/Sdata$y00_reg_hisp)

indx <- which(Sdata$y00_reg_hisp==0 | Sdata$y00_reg_hisp==999999 | 
Sdata$y06_turn_hisp==999999 | Sdata$y00_turn_hisp==999999 )

YSt1[indx] <- NA
YSt0[indx] <- NA

Y1m.Tr<-YSt1[m2$index.treated]
Y1m.Co<-YSt1[m2$index.control]
Y1m  <- c(Y1m.Tr,Y1m.Co)
Tr1m <- c(rep(TRUE,length(Y1m.Tr)),rep(FALSE,length(Y1m.Co)))

Y0m.Tr<-YSt0[m2$index.treated]
Y0m.Co<-YSt0[m2$index.control]
Y0m  <- c(Y0m.Tr,Y0m.Co)
Tr1m <- c(rep(TRUE,length(Y0m.Tr)),rep(FALSE,length(Y0m.Co)))

mddTr <- mean(na.rm=T,Y1m.Tr) - mean(na.rm=T,Y0m.Tr)
mddCo <- mean(na.rm=T,Y1m.Co) - mean(na.rm=T,Y0m.Co)
mdd <- mddTr - mddCo
print(mdd)

YY <- c(Y1m.Tr, Y1m.Co,Y0m.Tr, Y0m.Co)
time <- c(rep(1,length(c(Y1m.Tr, Y1m.Co))),rep(0,length(c(Y0m.Tr, Y0m.Co))))
TT <- c(rep(1,length(Y1m.Tr)),rep(0,length(Y1m.Co)),rep(1,length(Y0m.Tr)),rep(0,length(Y0m.Co)))
lmout = summarize.r(lm(YY~time+TT+I(time*TT)))
print(lmout)

DTr = Y1m.Tr - Y0m.Tr
DCo = Y1m.Co - Y0m.Co
D1 = Y1m.Tr - Y1m.Co
D0 = Y0m.Tr - Y0m.Co

t = t.test(DTr, DCo)
cat("T-test:\n");  print(t)
ks = ks.boot(DTr, DCo)
cat("KS Boot:\n"); print(ks)


RESACT[[3]]["Hispanic Turnout 2006-2000 Difference AM","Mean Diff"] = lmout$coefficients["I(time * TT)","Estimate"]
RESACT[[3]]["Hispanic Turnout 2006-2000 Difference AM","Diff means p-val"] = lmout$coefficients["I(time * TT)","Pr(>|t|)"]


######################################################
### (iii) Diff-in-Diff results on NonHispanic Registration
######################################################

#MATCHED  
   

YSt1 <- (Sdata$y06_reg_nhisp/Sdata$nhvap)
YSt0 <- (Sdata$y00_reg_nhisp/Sdata$nhvap)

indx <- which(Sdata$nhvap==0 | Sdata$nhvap==999999 |
Sdata$y06_reg_nhisp==999999 | Sdata$y00_reg_nhisp==999999 )

YSt1[indx] <- NA
YSt0[indx] <- NA
      
print("Percent Overrun")  
print(rbind(c('2006','2000'),round(c(length(which(YSt1>1))/length(YSt1),length(which(YSt0>1))/length(YSt0)),digits=4)))

YSt1[which(YSt1>1)] <- NA
YSt0[which(YSt0>1)] <- NA

Y1m.Tr<-YSt1[m2$index.treated]
Y1m.Co<-YSt1[m2$index.control]
Y1m  <- c(Y1m.Tr,Y1m.Co)
Tr1m <- c(rep(TRUE,length(Y1m.Tr)),rep(FALSE,length(Y1m.Co)))

Y0m.Tr<-YSt0[m2$index.treated]
Y0m.Co<-YSt0[m2$index.control]
Y0m  <- c(Y0m.Tr,Y0m.Co)
Tr1m <- c(rep(TRUE,length(Y0m.Tr)),rep(FALSE,length(Y0m.Co)))

mddTr <- mean(na.rm=T,Y1m.Tr) - mean(na.rm=T,Y0m.Tr)
mddCo <- mean(na.rm=T,Y1m.Co) - mean(na.rm=T,Y0m.Co)
mdd <- mddTr - mddCo
print(mdd)

YY <- c(Y1m.Tr, Y1m.Co,Y0m.Tr, Y0m.Co)
time <- c(rep(1,length(c(Y1m.Tr, Y1m.Co))),rep(0,length(c(Y0m.Tr, Y0m.Co))))
TT <- c(rep(1,length(Y1m.Tr)),rep(0,length(Y1m.Co)),rep(1,length(Y0m.Tr)),rep(0,length(Y0m.Co)))
lmout = summarize.r(lm(YY~time+TT+I(time*TT)))
print(lmout)

DTr = Y1m.Tr - Y0m.Tr
DCo = Y1m.Co - Y0m.Co
D1 = Y1m.Tr - Y1m.Co
D0 = Y0m.Tr - Y0m.Co

t = t.test(DTr, DCo)
cat("T-test:\n");  print(t)
ks = ks.boot(DTr, DCo)
cat("KS Boot:\n"); print(ks)


RESACT[[3]]["NonHispanic Registration 2006-2000 Difference AM","Mean Diff"] = lmout$coefficients["I(time * TT)","Estimate"]
RESACT[[3]]["NonHispanic Registration 2006-2000 Difference AM","Diff means p-val"] = lmout$coefficients["I(time * TT)","Pr(>|t|)"]


######################################################
### (iv) Diff-in-Diff results on NonHispanic Turnout
######################################################

#MATCHED

YSt1 <- (Sdata$y06_turn_nhisp/Sdata$y00_reg_nhisp)
YSt0 <- (Sdata$y00_turn_nhisp/Sdata$y00_reg_nhisp)

indx <- which(Sdata$y00_reg_nhisp==0 | Sdata$y00_reg_nhisp==999999 |
Sdata$y06_turn_nhisp==999999 | Sdata$y00_turn_nhisp==999999 )

YSt1[indx] <- NA
YSt0[indx] <- NA

Y1m.Tr<-YSt1[m2$index.treated]
Y1m.Co<-YSt1[m2$index.control]
Y1m  <- c(Y1m.Tr,Y1m.Co)
Tr1m <- c(rep(TRUE,length(Y1m.Tr)),rep(FALSE,length(Y1m.Co)))

Y0m.Tr<-YSt0[m2$index.treated]
Y0m.Co<-YSt0[m2$index.control]
Y0m  <- c(Y0m.Tr,Y0m.Co)
Tr1m <- c(rep(TRUE,length(Y0m.Tr)),rep(FALSE,length(Y0m.Co)))

mddTr <- mean(na.rm=T,Y1m.Tr) - mean(na.rm=T,Y0m.Tr)
mddCo <- mean(na.rm=T,Y1m.Co) - mean(na.rm=T,Y0m.Co)
mdd <- mddTr - mddCo
print(mdd)

YY <- c(Y1m.Tr, Y1m.Co,Y0m.Tr, Y0m.Co)
time <- c(rep(1,length(c(Y1m.Tr, Y1m.Co))),rep(0,length(c(Y0m.Tr, Y0m.Co))))
TT <- c(rep(1,length(Y1m.Tr)),rep(0,length(Y1m.Co)),rep(1,length(Y0m.Tr)),rep(0,length(Y0m.Co)))
lmout = summarize.r(lm(YY~time+TT+I(time*TT)))
print(lmout)

DTr = Y1m.Tr - Y0m.Tr
DCo = Y1m.Co - Y0m.Co
D1 = Y1m.Tr - Y1m.Co
D0 = Y0m.Tr - Y0m.Co

t = t.test(DTr, DCo)
cat("T-test:\n");  print(t)
ks = ks.boot(DTr, DCo)
cat("KS Boot:\n"); print(ks)


RESACT[[3]]["NonHispanic Turnout 2006-2000 Difference AM","Mean Diff"] = lmout$coefficients["I(time * TT)","Estimate"]
RESACT[[3]]["NonHispanic Turnout 2006-2000 Difference AM","Diff means p-val"] = lmout$coefficients["I(time * TT)","Pr(>|t|)"]

               
print(RESACT)

################## LATEX TABLES ##################
library(xtable)
library(stringr)      

baseline=cbind(RESACT[[1]][1:4,],RESACT[[2]][1:4,])
matched=cbind(RESACT[[1]][5:8,],RESACT[[2]][5:8,])

rownames(matched)=rownames(baseline)=c('Hispanic Turnout','Hispanic Registration','Non Hispanic Turnout','Non Hispanic Registration')     
  
print('--------------------')
print('Baseline')
print('--------------------')  
xtable(baseline,digits=c(1,3,2,3,2)) 

print('--------------------')
print('Matched')
print('--------------------')  
xtable(matched,digits=c(1,3,2,3,2))
                                              
################## LATEX TABLES ##################